package de.tabuma.lib.cm.crud;

import de.tabuma.lib.contentmanagement.*;
import de.tabuma.lib.contentmanagement.entity.Content;
import javax.servlet.http.*;
import de.tabuma.lib.*;
import java.util.*;
import java.util.logging.*;
import java.lang.reflect.*;


/**
 *
 * @author alefen
 */
public class UpdateViewFacade {
    private static final Logger log = Logger.getLogger((UpdateViewFacade.class.getName()));

    public static String view(Content content, HttpServletRequest request) {
        try {

            String cancel = "cancel";
            String update = "update";

            String str = "";

            Object object =  request.getAttribute(constants.OBJECT);

            str += "<div class=\"contentContent\">";
            
            if(request.getAttribute(constants.MESSAGE_ERROR)!=null) {
                str += "<div class=\"errormessage\"><div class=\"errormessage2\">"+request.getAttribute(constants.MESSAGE_ERROR)+"</div></div>\n";
            }
            if(request.getAttribute(constants.MESSAGE_INFO)!=null) {
                str += "<div class=\"infomessage\"><div class=\"infomessage2\">"+request.getAttribute(constants.MESSAGE_INFO)+"</div></div>\n";
            }

            str += view.getFormBeginn(content.GeneralContent.getServlet(), null);
            str += "<div class=\"cols\">";

            for(int i=0;i<content.FormContent.getEditableFields().size();i++) {
                Field myField = content.FormContent.getEditableFields().get(i);
                String methodName = ObjectMethods.getGetter(myField.getName());
                Integer type = content.FormContent.getEditableFieldTypes().get(i);
                String displayedName = content.FormContent.getDisplayedName().get(myField);
                int printType = 0;
                switch(type.intValue()) {
                    case constants.TYPE_LIST:
                        printType = view.TYPE_LIST;
                        break;
                    case constants.TYPE_LIST_STRING:
                        printType = view.TYPE_LIST_STRING;
                        break;
                    case constants.TYPE_PASSWORD:
                        printType = view.TYPE_PASSWORD;
                        break;
                    case constants.TYPE_STRING:
                        printType = view.TYPE_FIELD;
                        break;
                    case constants.TYPE_TEXTAREA:
                        printType = view.TYPE_TEXTAREA;
                        break;
                    case constants.TYPE_BOOLEAN:
                        printType = view.TYPE_CHECKBOX;
                        break;
                    default:
                        printType = view.TYPE_FIELD;
                        break;
                }

                String labelClass = null;
                String fieldClass = null;
                if(request.getAttribute(constants.ERROR_FIELD+myField.getName())!=null) {
                    labelClass = "error";
                    fieldClass = "errorfield";
                }

                if(i%2==0) {
                    str += view.printForm1(displayedName, myField.getName(), "" + object.getClass().getMethod(methodName).invoke(object), printType, content.ListContent.getListParameterContent().get(myField), content.ListContent.getListParameterDisplayedField().get(myField), labelClass, fieldClass);
                }
                else {
                    str += view.printForm2(displayedName, myField.getName(), "" + object.getClass().getMethod(methodName).invoke(object), printType, content.ListContent.getListParameterContent().get(myField), content.ListContent.getListParameterDisplayedField().get(myField), labelClass, fieldClass);
                }
            }
            if(content.FormContent.getEditableFields().size()%2==1) {
                str += "<div class=\"col2\">\n";
                str += "<div class=\"col2_head\"></div>\n";
                str += "<div class=\"col2_content\"></div>\n";
                str += "</div>\n";
            }
            str += "</div>";
            str += view.getHiddenAsAttribute(content.GeneralContent.getParameter());
            str += view.printHiddenField(content.GeneralContent.getParamName(), ""+constants.UPDATE);
            str += view.printHiddenField(constants.UPDATE1, "true");
            str += view.printHiddenField(content.GeneralContent.getParamName()+constants.id, ""+object.getClass().getMethod("getId").invoke(object));
            str += "<input type=\"submit\" name=\"submit\" class=\"button\" value=\""+update+" "+content.FormContent.getEntityName()+"\"/>";
            str += "<input type=\"submit\" name=\"" + constants.CANCEL + "\" class=\"button\" value=\""+cancel+"\"/>";
            str += view.getFormEnd();
            str += "</div>";
            return str;
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return "";
    }
}